﻿using System;
using System.Linq;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using FranceTriple.Dal;
using System.Web.Routing;

namespace TeamFranceTriple
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        public String RequesterCulture { get; set; }
        public UserProfile RequesterProfile { get; private set; }
        public MembershipUser RequesterMembership { get; private set; }
        public String[] RequesterRoles { get; private set; }
        public Boolean RequesterIsAuthenticated { get; private set; }
        public String RequesterIpAddress { get; private set; }
        public String RequesterTimeZone { get; set; }

        private String AdressIp;
        private HtmlLink newStyleSheet, newStyleSheet2;
        private HtmlMeta metaDescription, metaKeywords, metaAuthor, metaGenerator;
        private String MetaDescription = String.Empty;

        private string GetUserIp
        {
            get
            {
                //Retrouver l'adresse d'origine, même si le visiteur est derriere un proxy
                AdressIp = Request.ServerVariables["REMOTE_HOST"].ToString();
                if (Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
                    AdressIp = Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

                //Afin d'éviter un petit bug aux développeurs qui ont l'ip v6
                if (AdressIp == "::1")
                    AdressIp = "127.0.0.1";

                return AdressIp;
            }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            if (Session["AnonymousID"] == null)
            {
                //Centralise le membership et le profile utilisateur dans une propriété publique
                //Met à jour la date de dernière activité
                RequesterMembership = Membership.GetUser(true);
            }
            RequesterProfile = UserProfile.GetUserProfile();
            RequesterRoles = Roles.GetRolesForUser();
            RequesterIsAuthenticated = Page.User.Identity.IsAuthenticated;
            RequesterIpAddress = this.GetUserIp;
            RequesterTimeZone = RequesterProfile.TimeZone;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            FrenchLanguageHyperLink.NavigateUrl = Request.RawUrl.ToLower().Replace("en-us", "fr-fr");
            EnglishLanguageHyperLink.NavigateUrl = Request.RawUrl.ToLower().Replace("fr-fr", "en-us");

            HomeLink1.NavigateUrl = "~/" + RequesterCulture + "/home/";
            ForumLink1.NavigateUrl = "~/" + RequesterCulture + "/forum/";
            EventLink1.NavigateUrl = "~/" + RequesterCulture + "/eventlist/";
            PictureLink1.NavigateUrl = "~/" + RequesterCulture + "/picturelist/";
            FriendlyTeamLink1.NavigateUrl = "~/" + RequesterCulture + "/friendlyteam/";
            MemberLink1.NavigateUrl = "~/" + RequesterCulture + "/memberlist/";
            AboutLink1.NavigateUrl = "~/" + RequesterCulture + "/about/";
            FanLink1.NavigateUrl = "~/" + RequesterCulture + "/fanlist/";

            SignupLink.NavigateUrl = "~/" + RequesterCulture + "/register/";
            SignupLink2.NavigateUrl = "~/" + RequesterCulture + "/register/";

            PictureLink1.Visible = RequesterRoles.Contains("Member");
            EventLink1.Visible = RequesterRoles.Contains("Member");

            SignupLink.Visible = (Session["AnonymousID"] != null);
            SignupLink2.Visible = (Session["AnonymousID"] != null);

            metaDescription = new HtmlMeta();
            metaDescription.Name = "DESCRIPTION";
            metaDescription.Content = "MetaDescription".ToLocalizedString().TextToHtml(); 
            Page.Header.Controls.Add(metaDescription);

            metaKeywords = new HtmlMeta();
            metaKeywords.Name = "KEYWORDS";
            metaKeywords.Content = "metaKeywords".ToLocalizedString().TextToHtml();
            Page.Header.Controls.Add(metaKeywords);

            metaAuthor = new HtmlMeta();
            metaAuthor.Name = "AUTHOR";
            metaAuthor.Content = "metaAuthor".ToLocalizedString().TextToHtml();
            Page.Header.Controls.Add(metaAuthor);

            metaGenerator = new HtmlMeta();
            metaGenerator.Name = "GENERATOR";
            metaGenerator.Content = "metaGenerator".ToLocalizedString().TextToHtml();
            Page.Header.Controls.Add(metaGenerator);

            // Ajouter la feuille de style commune
            newStyleSheet = new HtmlLink();
            newStyleSheet.Href = "~/Styles/Site.css";
            newStyleSheet.Attributes.Add("type", "text/css");
            newStyleSheet.Attributes.Add("rel", "stylesheet");

            // Selectionner automatiquement une feuille de style sans fond d'écran pour les mobiles et avec pour les autres
            newStyleSheet2 = new HtmlLink();
            if (Request.Browser.IsMobileDevice)
            {
                newStyleSheet2.Href = "~/Styles/Site_Mobile.css";
            }
            else
            {
                newStyleSheet2.Href = "~/Styles/Site_Pc.css";
            }
            newStyleSheet2.Attributes.Add("type", "text/css");
            newStyleSheet2.Attributes.Add("rel", "stylesheet");

            Page.Header.Controls.Add(newStyleSheet);
            Page.Header.Controls.Add(newStyleSheet2);
        }
    }
}
